BEGIN;
	CREATE FUNCTION estimate_aerial_distance(DOUBLE PRECISION,DOUBLE PRECISION,DOUBLE PRECISION,DOUBLE PRECISION) RETURNS DOUBLE PRECISION AS $$
	--Shamelessly stolen from http://andrew.hedges.name/experiments/haversine/
		WITH step1 AS (SELECT $4-$2 AS dlat, $3-$1 AS dlng)
			,step2 AS (SELECT sin(dlat/2)^2 + cos($1)*cos($3)*sin(dlng/2)^2 AS a FROM step1)
			,step3 AS (SELECT 2*atan2(sqrt(a),sqrt(1-a))*6371 AS d FROM step2)
		SELECT d FROM step3
	$$ LANGUAGE sql;
END;
